package com.xiyue.leaspring.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.xiyue.leaspring.dao.IDeptDAO_PagingAndSortingRepository;
import com.xiyue.leaspring.po.Dept;
import com.xiyue.leaspring.service.IDeptService_PagingAndSortingRepository;

@Service
public class IDeptServiceImpl_PagingAndSortingRepository implements IDeptService_PagingAndSortingRepository {

	
	@Autowired
	private IDeptDAO_PagingAndSortingRepository iDeptDAO_PagingAndSortingRepository;
	
	@Override
	public Map<String, Object> listPageSort(int curentPage, int lineSize) {
		// TODO Auto-generated method stub
		Sort sort = new Sort(Sort.Direction.DESC,"deptno");//设置部门编号deptno降序
		//将分页与排序操作保存到PageRequest接口对象中,通过DAO层进行方法调用，页数从0开始
		Pageable pageable = PageRequest.of(curentPage-1, lineSize,sort);
		//Page会自动保存全部记录，总记录数，以及总页数
		Page<Dept> pageDept = this.iDeptDAO_PagingAndSortingRepository.
				findAll(pageable);//数据查询
		Map<String,Object> map = new HashMap<String, Object>();//保存返回结果
		map.put("allDepts", pageDept.getContent());//保存全部记录
		map.put("deptCount", pageDept.getTotalElements());//保存总记录数
		map.put("deptPage",pageDept.getTotalPages());//保存总页数
		return map;
	}

	@Override
	public List<Dept> list() {
		// TODO Auto-generated method stub
		Sort sort = new Sort(Sort.Direction.DESC,"deptno");//设置部门编号deptno降序
		return (List<Dept>) this.iDeptDAO_PagingAndSortingRepository.findAll(sort);//排序查询
	}

}
